package com.zfzs.post.controller;

import com.zfzs.post.aop.log.Log;
import com.zfzs.post.common.bean.ResponseResult;
import com.zfzs.post.model.dto.IdDTO;
import com.zfzs.post.model.dto.InsertAgentAreaDTO;
import com.zfzs.post.model.dto.UpdateAgentAreaDTO;
import com.zfzs.post.model.vo.AddressVo;
import com.zfzs.post.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 地址信息
 *
 * @author: ck
 * @create: 2019/9/9/16:41
 **/
@Slf4j
@RestController
@RequestMapping("address/")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 获取省列表
     */
    @Log("获取省列表")
    @PostMapping(value = "get_province_list")
    public ResponseResult queryProvinceList() {
        Map<String, Object> map = addressService.queryProvinceList();
        return ResponseResult.ok(map);
    }

    /**
     * 查询市或者区列表
     */
    @Log("查询市或者区列表")
    @PostMapping(value = "query_city_or_area")
    public ResponseResult queryCityOrArea(@Validated @RequestBody IdDTO dto) {
        Map<String, Object> map = addressService.queryCityOrArea(dto);
        return ResponseResult.ok(map);
    }

    /**
     * 添加收货地址
     */
    @Log("添加收货地址")
    @PostMapping(value = "insert_address")
    public ResponseResult insertAddress(@Validated @RequestBody InsertAgentAreaDTO dto) {
        addressService.insertAddress(dto);
        return ResponseResult.ok();
    }

    /**
     * 修改地址
     */
    @Log("修改地址")
    @PostMapping(value = "update_address")
    public ResponseResult updateAddress(@Validated @RequestBody UpdateAgentAreaDTO dto) {
        addressService.updateAddress(dto);
        return ResponseResult.ok();
    }

    /**
     * 删除地址
     */
    @Log("删除地址")
    @PostMapping(value = "delete_address")
    public ResponseResult deleteAddress(@Validated @RequestBody IdDTO dto) {
        addressService.deleteAddress(dto);
        return ResponseResult.ok();
    }

    /**
     * 查询用户地址列表
     */
    @Log("查询用户地址列表")
    @PostMapping(value = "get_area_list")
    public ResponseResult queryAreaList() {
        Map<String, Object> map = addressService.queryAreaList();
        return ResponseResult.ok(map);
    }

    /**
     * 取消默认
     */
    @Log("取消默认")
    @PostMapping(value = "cancel_default")
    public ResponseResult cancelDefault(@Validated @RequestBody IdDTO dto) {
        addressService.cancelDefault(dto);
        return ResponseResult.ok();
    }

    /**
     * 设置默认
     */
    @Log("设置默认")
    @PostMapping(value = "setting_default")
    public ResponseResult settingDefault(@Validated @RequestBody IdDTO dto) {
        addressService.settingDefault(dto);
        return ResponseResult.ok();
    }

    /**
     * 获取默认
     */
    @Log("获取默认")
    @PostMapping(value = "get_default_address")
    public ResponseResult getDefaultAddress() {
        AddressVo addressVo = addressService.getDefaultAddress();
        if (addressVo == null) {
            return ResponseResult.ok(new HashMap<>());
        } else {
            return ResponseResult.ok(addressVo);
        }
    }


}
